import { defaultHeaders, download as downloadFile, httpDelete, httpGet } from '@/http'
import { uploadUrl, downloadUrl, services } from '@/config'
import { fileCenterService } from '@/api/constants'
import { normalizeUrl } from '@web-utils/core'

/**
 * 获取附件信息
 * @param {any} parentUnid 所属文件的unid
 * @param {any} parentType 所属文件的type
 * @returns {*}
 */
export const findAllByParentUnid = (parentUnid, parentType) =>
  httpGet(`${fileCenterService}attachment/list`, { parentUnid, parentType })

export const getAttachments = (unid, { classify, model, categories }) =>
  httpGet(`${services.adminService}sys-file/page`, {
    descs: 'create_time',
    classify,
    categories,
    model,
    current: 1,
    size: 1000,
    uniqueCode: unid
  })

/**
 * 单文件上传地址
 */
export const getUploadUrl = () => `${services.adminService}sys-file/upload`

/**
 * 单文件上传地址(不分片),无需认证
 */
export const getUploadUrlNocert = () => `${uploadUrl}/${fileCenterService}attachment/upload/nocert`


/**
 * 获取附件上传头信息
 */
export const getHeader = () => {
  const headers = {
    ...defaultHeaders()
  }
  return headers
}

/**
 * 删除附件
 * @param {any} id 附件id
 */
export const remove = id => httpDelete(`${services.adminService}sys-file/${id}`)

/**
 * 分片上传
 * @returns {string}
 */
export const chunk = () => normalizeUrl(`${process.env.VUE_APP_PUBLIC_PATH}${services.commonService}file/chunk`)

/**
 * 文件下载
 * @param ids 多个用逗号隔开
 * @param options
 */
export const download = (ids, options = {}) =>
  downloadFile(`${downloadUrl}/${fileCenterService}attachment/download?ids=${ids}`, options)
